package com.itfreer.data.general.interceptor.defulat;

import java.util.ArrayList;
import java.util.List;

import com.itfreer.data.general.IQueryMap;
import com.itfreer.data.general.ITable;
import com.itfreer.data.general.QueryMap;
import com.itfreer.data.general.RowBase;
import com.itfreer.data.general.domain.DomainType;
import com.itfreer.data.general.interceptor.DbEditInterceptorClass;
import com.itfreer.data.general.interceptor.DbEditType;
import com.itfreer.utils.StringUtils;

/**
 * 类功能说明 定义树型表操作拦截器
 */
public class TreeTableEditInterceptorClass extends DbEditInterceptorClass {

	public TreeTableEditInterceptorClass() {
		super.setIndex(-70);
	}

	@Override
	public void BeforeDbEditOption(ITable pTable, Object entitys,
			DbEditType editType) {
		if (editType == DbEditType.Delete && 
			(pTable.getDomainType() == DomainType.BusinessTable || pTable.getDomainType() == DomainType.DomainTable)
			&& StringUtils.IsNullOrSpace(pTable.getIDFieldName())) {
			String idField = pTable.getIDFieldName();
			String pIDField = pTable.getPIDFieldName();
			List<Object> resultList = new ArrayList<Object>();
			if (entitys instanceof RowBase) {
				getNextIDs(resultList, pTable, (RowBase) entitys, idField,
						pIDField);
			} else if (entitys instanceof IQueryMap) {
				getNextIDs(resultList, pTable, (IQueryMap) entitys,
						idField, pIDField);
			}
			if(resultList.size()>0){
				String fValue = "";
				for (Object idObject : resultList) {
					if (idObject == null) {
						continue;
					}
					fValue += "'" + idObject.toString() + "',";
				}
				if (fValue != "") {
					fValue = fValue.substring(0, fValue.length() - 1);
				}
				IQueryMap queryMap = new QueryMap();
				queryMap.getWhereMap().put(idField + ":in", fValue);
				pTable.getWorkspace().delete(pTable, queryMap);
			}
		}
	}

	@Override
	public void AfterDbEditOption(ITable pTable, Object entitys,
			DbEditType editType) {
	}

	protected void getNextIDs(List<Object> resultList, ITable pTable,
			IQueryMap queryMap, String idField, String pIDField) {
		if(queryMap==null){
			return;
		}
		List<RowBase> rowList = pTable.getEntityList(RowBase.class, queryMap);
		if (rowList != null && rowList.size() > 0) {
			for (RowBase item : rowList) {
				getNextIDs(resultList, pTable, item, idField, pIDField);
			}
		}
	}

	protected void getNextIDs(List<Object> resultList, ITable pTable,
			RowBase row, String idField, String pIDField) {
		if (row == null) {
			return;
		}
		Object idObject = row.getValue(idField);
		IQueryMap queryMap = new QueryMap();
		queryMap.setSelectFields(idField);
		queryMap.getWhereMap().put(pIDField, idObject);
		List<RowBase> rowList = pTable.getEntityList(RowBase.class, queryMap);
		for (RowBase item : rowList) {
			idObject = item.getValue(idField);
			resultList.add(idObject);
			getNextIDs(resultList, pTable, item, idField, pIDField);
		}
	}

	@Override
	public void DbEditOptionException(ITable table, Object editEntitys,
			DbEditType editType, Exception ex) {
	}

	@Override
	public String getInterceptorName() {
		return "定义树型表操作拦截器";
	}
}